package cetcbigdata.da.legal_person_service.beijing;

import cetcbigdata.da.base.Base;
import cn.wanghaomiao.xpath.model.JXDocument;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.nodes.Element;

import java.util.ArrayList;
import java.util.List;

/**
 * @author YuanGongPing
 * @version 0.1
 * @description BeijingPersonalCorporateMattersGuide
 * @since 2022/7/21 15:20
 */

public class SpidersBeiJing extends Base {

    private static String region = "北京市";

    private static String module = "法人事项";

    @Override
    public JSONObject getDataDemo() {
        try {
            return processResult("xxx", "xxx", "xxx", "xxx", new JSONObject(), "xxx", "xxx");
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {
        for(JSONObject regionItem : BeijingUtil.getRegionList()){
            for(JSONObject topicItem : BeijingUtil.getTopic() ){
                this.parseListPage(recordSender, fieldsList, regionItem, topicItem);
            }
        }
    }


    private void parseListPage(RecordSender recordSender, List<Object> fieldsList, JSONObject regionItem, JSONObject topicItem){
        // 当前页
        int currentPage = 1;
        // 如果当前也不等于总页码数，则一直循环 ，实现翻页
        while (true){
            try {
                String listUrl = "http://banshi.beijing.gov.cn/guideservice/pubtask/getTaskList";
                JSONObject headers = new JSONObject();
                headers.put("Referer", "http://banshi.beijing.gov.cn");
                headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36");

                JSONObject params = new JSONObject();
                // 1002 代表法人服务， 1001 代表个人服务
                params.put("serverType", "1002");
                params.put("locationCode", regionItem.getString("id"));
                params.put("localType", "0");
                params.put("businessType", "");
                params.put("topicType", topicItem.getString("id"));
                params.put("taskName", "");
                params.put("deptCode", "");
                params.put("taskType", "");
                params.put("pageNum" , currentPage);


                JSONObject parmas = new JSONObject();
                String listResponse =  getReq(listUrl, headers, parmas);
                JSONObject responseObj = JSONObject.parseObject(listResponse);
                if(responseObj.getString("code").equals("200")){
                    JSONArray list = responseObj.getJSONObject("data").getJSONArray("list");
                    // 如果没有数据返回，则停止翻页
                    if(list.size() == 0){
                        return;
                    }
                    for(Object o : list){
                        JSONObject item = (JSONObject) o;
                        // 如果存在子选项， 先采集父节点信息，然后再采集子节点信息，父节点信息只存在基本信息页面，子节点信息则和其他情况一样
                        if(item.containsKey("childNode")){
                            String detailUrl = String.format("http://banshi.beijing.gov.cn%s?locationCode=%s&serverType=%s",
                                    item.getString("taskUrl"),
                                    item.getString("locationCode"),
                                    "1002"
                            );
                            String detailResponse =  getReq(detailUrl, headers, parmas);

                            JSONObject content = this.parseDetailPageHasChildNode(detailResponse);
                            JSONArray childNodeList = item.getJSONArray("childNode");
                            JSONArray contentChildList = new JSONArray();
                            for(Object oChild : childNodeList){
                                JSONObject itemChild = (JSONObject) oChild;
                                String detailChildUrl = String.format("http://banshi.beijing.gov.cn%s?locationCode=%s&serverType=%s",
                                        itemChild.getString("taskUrl"),
                                        itemChild.getString("locationCode"),
                                        "1002"
                                );
                                String responseChildDetail =  getReq(detailChildUrl, headers, parmas);
                                JSONObject contentChild = this.parseDetailPage(responseChildDetail);
                                contentChildList.add(contentChild);
                            }
                            content.put("选项", contentChildList);
                            printDataCount(4);
                            processItem(
                                    processResult(listUrl, listResponse, detailUrl, detailResponse, content,
                                            region + "-" + regionItem.getString("locationName"),
                                            module + "-" + topicItem.getString("name")
                                    ),
                                    recordSender,
                                    fieldsList
                            );
                        }
                        else {
                            // 没有子选项情况
                            String detailUrl = String.format("http://banshi.beijing.gov.cn%s?locationCode=%s&serverType=%s",
                                    item.getString("taskUrl"),
                                    item.getString("locationCode"),
                                    "1002"
                            );
                            String detailResponse = getReq(detailUrl, headers, parmas);
                            JSONObject content = this.parseDetailPage(detailResponse);
                            printDataCount(4);
                            processItem(
                                    processResult(listUrl, listResponse, detailUrl, detailResponse, content,
                                            region + "-" + regionItem.getString("locationName"),
                                            module + "-" + topicItem.getString("name")
                                    ),
                                    recordSender,
                                    fieldsList
                            );
                        }
                    }
                }
                currentPage ++;
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    private JSONObject parseDetailPageHasChildNode(String html){
        try {
            // 基本信息
            JSONObject baseInfo = new JSONObject();
            JXDocument jxDocument = new JXDocument(html);
            List<Object> rs = jxDocument.sel("//*[@class='wai_box wai_box2']//td[@class='td-name']");
            for (Object o : rs){
                String key  = ((Element) o).text();
                String value = ((Element) o).nextElementSibling().text();
                baseInfo.put(key, value);
            }
            rs = jxDocument.sel("//p[@class='P_title']");
            String name = null;
            for (Object o : rs){
                name  = ((Element) o).text();
            }
            baseInfo.put("事项名称", name);
            return baseInfo;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    // 解析详情页信息
    private JSONObject parseDetailPage(String html){
        JSONObject res = new JSONObject();
        try {
            res.put("基本信息", BeijingUtil.getBaseInfo(html));
            res.put("办理流程", BeijingUtil.getHandlingProcedures(html));
            res.put("申请材料", BeijingUtil.getMaterialAll(html));
            res.put("结果名称及样本", BeijingUtil.getResult(html));
            res.put("设定依据", BeijingUtil.getApproval(html));
            res.put("收费标准及依据", BeijingUtil.getStandardList(html));
        }catch (Exception e){
            e.printStackTrace();
        }
        return res;
    }

    public static void main(String[] args) {
        SpidersBeiJing spidersBeiJing = new SpidersBeiJing();
        spidersBeiJing.getData(null, new ArrayList<>());
    }
}
